package com.zhugang.week14.concurrent;

import java.util.concurrent.TimeUnit;

/**
 * @program algorithms
 * @description: ReOrdering
 * @author: chanzhugang
 * @create: 2022/09/15 19:56
 */
public class ReOrdering {

    // 如果不加 volatile， 线程2不会打印
    volatile static int a = 0;

    public static void main(String[] arg) {
        Runnable r1 = () -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            a = 10000;
            System.out.println("set a");
        };
        Runnable r2 = () -> {
            while (a < 100) {

            }

            System.out.println("end:" + a);
        };
        new Thread(r1).start();
        new Thread(r2).start();
    }
}